package com.zkthink.ceres.promotion.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.ceres.common.enums.YesOrNoEnum;
import com.zkthink.ceres.promotion.entity.SalesRelationConfig;
import com.zkthink.ceres.promotion.dto.SalesRelationConfigSaveDTO;
import com.zkthink.ceres.promotion.dto.SalesRelationConfigUpdateDTO;
import com.zkthink.ceres.promotion.dto.SalesRelationConfigPageDTO;
import com.zkthink.ceres.promotion.service.SalesRelationConfigService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.zkthink.base.controller.SuperController;
import com.zkthink.base.R;
import com.zkthink.database.mybatis.conditions.Wraps;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zkthink.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 分销员
 * </p>
 *
 * @author kellen
 * @date 2020-08-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/salesRelationConfig")
@Api(value = "分销配置", tags = "分销配置")
//@PreAuth(replace = "salesRelationConfig:")
public class SalesRelationConfigController extends SuperController<SalesRelationConfigService, Long, SalesRelationConfig, SalesRelationConfigPageDTO, SalesRelationConfigSaveDTO, SalesRelationConfigUpdateDTO> {

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<SalesRelationConfig> salesRelationConfigList = list.stream().map((map) -> {
            SalesRelationConfig salesRelationConfig = SalesRelationConfig.builder().build();
            //TODO 请在这里完成转换
            return salesRelationConfig;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(salesRelationConfigList));
    }

    /**
     * 管理后台查询分销配置，如果没有数据，就默认初始化，其它地方查询需要考虑是否处理这种情况
     * @param page
     */
    @Override
    public void handlerResult(IPage<SalesRelationConfig> page) {
        if(page == null || page.getTotal() == 0){
            baseService.save(SalesRelationConfig.builder().isForeverValid(YesOrNoEnum.YES.getCode()).protectTime(30).rebindCustomerFlag(2).salesBindFalg(1).selfBuyReturnCommisionFlag(2).build());
            SalesRelationConfig salesRelationConfig = baseService.getOne(Wraps.<SalesRelationConfig>lbQ().eq(SalesRelationConfig::getIsDelete, YesOrNoEnum.NO.getCode()));
            page.setTotal(1);
            page.setRecords(Arrays.asList(salesRelationConfig));
            page.setPages(1);
        }
    }
}
